package com.ruoyi.StarStudio.Controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.StarStudio.Entity.*;
import com.ruoyi.StarStudio.Service.BeansActionService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/beansAction")
public class BeansActionController {

    @Autowired
    private BeansActionService beansActionService;

    /**
     * 条件查询 收支记录分页接口
     * @param beansAction 收支记录对象
     * @return 分页结果对象 PageResult
     */
    @GetMapping("/page")
    public PageResult page(BeansAction beansAction){
        PageUtils.startPage();
        List<BeansAction> beansActionList;
        long allRecordNumber;
        try {
            beansActionList = beansActionService.findBeansActionList(beansAction);
            allRecordNumber = beansActionService.findAllRecordNumber();
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR, MessageConstant.QUERY_PAGE_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS,MessageConstant.QUERY_PAGE_SUCCESS, new PageInfo<>(beansActionList).getTotal(), beansActionList, allRecordNumber);
    }

    /**
     * 导出收支记录到 Excel表格
     * @param response http响应
     * @param beansAction 收支记录对象
     */
    @Log(title = "收支记录管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BeansAction beansAction) {
        List<BeansAction> list = beansActionService.findBeansActionList(beansAction);
        ExcelUtil<BeansAction> util = new ExcelUtil<>(BeansAction.class);
        util.exportExcel(response, list, "鲜豆收支数据");
    }

    /**
     * 根据收支记录ID查找收支记录
     * @param id 收支记录ID
     * @return 结果对象 Result
     */
    @GetMapping("/{id}")
    public Result findBeansActionById(@PathVariable long id){
        BeansAction byBeansActionId;
        try {
            byBeansActionId = beansActionService.findByBeansAction_id(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.QUERY_BEANS_ACTION_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.QUERY_BEANS_ACTION_SUCCESS, byBeansActionId);
    }

    /**
     * 根据用户ID查找收支记录
     * @param id 用户ID
     * @return 结果对象 Result
     */
    @GetMapping("/byUserId/{id}")
    public Result findBeansActionByUserId(@PathVariable long id){
        List<BeansAction> beansActionList;
        try {
            beansActionList = beansActionService.findByBeansAction_user_id(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.QUERY_BEANS_ACTION_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.QUERY_BEANS_ACTION_SUCCESS, beansActionList);
    }

    /**
     * 根据收支记录ID删除收支记录（可单个删除也可批量删除）
     * @param Ids 待删除的收支记录ID
     * @return 结果对象 Result
     */
    @Log(title = "收支记录管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public Result deleteBeansAction(@PathVariable String Ids){
        try {
            if (Ids.contains(",")){
                String[] Id = Ids.split(",");
                for (String IdStr : Id) {
                    beansActionService.deleteBeansActionById(Integer.parseInt(IdStr));
                }
            }else {
                beansActionService.deleteBeansActionById(Integer.parseInt(Ids));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.DELETE_BEANS_ACTION_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.DELETE_BEANS_ACTION_SUCCESS);
    }

    /**
     * 新增收支记录
     * @param beansAction 收支记录
     * @return 结果对象 Result
     */
    @Log(title = "收支记录管理", businessType = BusinessType.INSERT)
    @PostMapping
    public Result addBeansAction(@RequestBody BeansAction beansAction){
        try {
            beansActionService.addBeansAction(beansAction);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.ADD_BEANS_ACTION_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.ADD_BEANS_ACTION_SUCCESS);
    }

    /**
     * 修改收支记录
     * @param beansAction 收支记录
     * @return 结果对象 Result
     */
    @Log(title = "收支记录管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result updateBeansAction(@RequestBody BeansAction beansAction){
        try {
            beansActionService.updateBeansAction(beansAction);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.UPDATE_BEANS_ACTION_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.UPDATE_BEANS_ACTION_SUCCESS);
    }

}
